package com.icss.service.cmt;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.icss.helper.BatchSql;
import com.icss.service.BaseService;

/**
 * 报文处理
 * @author shixiaolong
 * @dateJun 12, 2012
 */
public class CmtService extends BaseService{
	
	public int save(HttpServletRequest request){
		String big_cash  = req.getValue(request, "big_cash");
		String little_cash  = req.getValue(request, "little_cash");
		BatchSql batchSql = new BatchSql();
		String sql="delete from tbl_cmt_config ";
		batchSql.addBatch(sql);
		sql="insert into tbl_cmt_config(big_cash ,little_cash) values(?,?)";
		batchSql.addBatch(sql,new Object[]{big_cash,little_cash});
		return db.doInTransaction(batchSql);
	}
	
	public Map getDetail(HttpServletRequest request){
		String sql="select * from tbl_cmt_config ";
		return db.queryForMap(sql);
	}
	
	/**
	 * 接收行列表
	 * @return
	 */
	public List<String> recBankList(HttpServletRequest request){
		String bank = req.getValue(request, "bank");
		request.setAttribute("bank", bank);
		String sql="select bank_name from tbl_bank where bank_name<>?";
		return db.queryForList(sql,new Object[]{bank});
	}
	
	public int cmtSendSave(HttpServletRequest request){
		System.out.println("-----------cmtSendSave--------------");
		String rec_bank = req.getValue(request, "rec_bank");
		String send_bank = req.getValue(request, "send_bank");
		String cmt_type = req.getValue(request, "cmt_type");
		String audit_type = req.getValue(request, "audit_type");
		BatchSql batchSql = new BatchSql();
		if("cmt100".equals(cmt_type)){
			String cash =req.getValue(request,"cash");
			String beginc_bankcode =req.getValue(request,"beginc_bankcode");
			String begin_bankcode =req.getValue(request,"begin_bankcode");
			String send_bankcode =req.getValue(request,"send_bankcode");
			String send_account =req.getValue(request,"send_account");
			String send_name =req.getValue(request,"send_name");
			String send_address =req.getValue(request,"send_address");
			String receivec_bankcode =req.getValue(request,"receivec_bankcode");
			String receive_bankcode =req.getValue(request,"receive_bankcode");
			String rec_bankcode =req.getValue(request,"rec_bankcode");
			String rec_account =req.getValue(request,"rec_account");
			String rec_name =req.getValue(request,"rec_name");
			String rec_address =req.getValue(request,"rec_address");
			String business =req.getValue(request,"business");
			String pay_code =req.getValue(request,"pay_code");
			String remark =req.getValue(request,"remark");
			
			String cmt_id = db.queryForString("select TBL_CMT100_s_id.nextval from dual");
			
			String sql="insert into tbl_cmt100(cmt_id, cmt_date, cash, beginc_bankcode, " +
					"begin_bankcode, send_bankcode, send_account, send_name, send_address," +
					" receivec_bankcode, receive_bankcode, rec_bankcode, rec_account," +
					" rec_name, rec_address, business, pay_code, remark) values(?,sysdate,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
			batchSql.addBatch(sql,new Object[]{cmt_id,cash, beginc_bankcode,begin_bankcode, send_bankcode, send_account, 
					send_name, send_address,receivec_bankcode, receive_bankcode, rec_bankcode, rec_account,
					rec_name, rec_address, business, pay_code, remark});
			String main_id = db.queryForString("select tbl_cmt_main_s_id.nextval from dual");
			sql="insert into tbl_cmt_main(main_id,cmt_id,audit_type,cmt_type) values(?,?,?,?)";
			batchSql.addBatch(sql,new Object[]{main_id,cmt_id,audit_type,cmt_type});
			if("1".equals(audit_type)){
				//批量审核
				sql="insert into TBL_CMT_ROUTE(route_id,main_id,from_id,to_id,begin_date,done_flag,done_date) values(TBL_CMT_ROUTE_s_id.nextval,?,?,?,sysdate,1,sysdate)";
				batchSql.addBatch(sql,new Object[]{main_id,send_bank,rec_bank});
			}else{
				//人工审核
			}
			//发送行减去金额
			sql="update tbl_bank set bank_cash=bank_cash-? where bank_name = ? ";
			batchSql.addBatch(sql,new Object[]{cash,send_bank});
			//接收行增加金额
			sql="update tbl_bank set bank_cash=bank_cash+? where bank_name = ? ";
			batchSql.addBatch(sql,new Object[]{cash,rec_bank});
			
		}else if("cmt101".equals(cmt_type)){
			
		}else if("cmt102".equals(cmt_type)){
			
		}else if("cmt103".equals(cmt_type)){
			
		}else if("cmt105".equals(cmt_type)){
			
		}else if("cmt108".equals(cmt_type)){
			
		}
		return db.doInTransaction(batchSql);
	}
	
	/**
	 * 小额大额审核列表
	 * @param request
	 * @return
	 */
	public List<Map> cmtAuditList(HttpServletRequest request){
		String sql="";
		return null;
		
	}
	
	public List<Map> getBankList(HttpServletRequest request){
		String sql="select * from tbl_bank";
		return db.queryForList(sql);
		
	} 
}
